package view.micro
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import model.micro.MicroFortress;
	
	/**
	 * Represents a fortress in a battle.
	 */
	public class FortressSprite extends Sprite implements IMicroSprite
	{
		/**The fortress this sprite represents.*/
		private var fortress:MicroFortress;
		
		private var health_bar:Shape;
		private var archer_count:TextField;
		
		/**
		 * Create a new FortressSprite to represent a fortress.
		 * @param	fortress	The fortress to represent
		 */
		public function FortressSprite(fortress:MicroFortress)
		{
			this.fortress = fortress;
			addEventListener(Event.ADDED, init, false, 0, true);
		}
		
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED, init);
			
			x = fortress.getBoundingBox().left;
			y = fortress.getBoundingBox().bottom;
			
			graphics.beginFill(0xAAAAAA);
			graphics.lineStyle(2, 0x999999);
			graphics.drawRect(0, 0, fortress.getBoundingBox().width, fortress.getBoundingBox().height);
			
			health_bar = new Shape();
			health_bar.graphics.beginFill(0xFF0000);
			health_bar.graphics.drawRect(0, 0, fortress.getBoundingBox().width, 10);
			health_bar.graphics.endFill();
			health_bar.y = fortress.getBoundingBox().height + 10;
			addChild(health_bar);
			
			archer_count = new TextField();
			archer_count.text = "adsfasdfasdf";
			archer_count.x = 20;
			archer_count.y = 20;
			addChild(archer_count);
		}
		
		/**
		 * Update the sprite to match the state of the fortress.
		 */
		public function update():void
		{
			health_bar.scaleX = (1.0 * fortress.getHP()) / fortress.getMaxHP();
			
			archer_count.text = "234";
		}
		
		public function readyToRemove():Boolean
		{
			return fortress.getHP() <= 0;
		}
	
	}

}